/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.representativeWork.service.impl;

import co.yixiang.api.ApiResult;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.representativeWork.domain.YxRepresentativeWork;
import co.yixiang.modules.representativeWork.service.YxRepresentativeWorkService;
import co.yixiang.modules.representativeWork.service.dto.YxRepresentativeWorkDto;
import co.yixiang.modules.representativeWork.service.dto.YxRepresentativeWorkQueryCriteria;
import co.yixiang.modules.representativeWork.service.mapper.YxRepresentativeWorkMapper;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-08-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxRepresentativeWork")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxRepresentativeWorkServiceImpl extends BaseServiceImpl<YxRepresentativeWorkMapper, YxRepresentativeWork> implements YxRepresentativeWorkService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxRepresentativeWorkQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxRepresentativeWork> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxRepresentativeWorkDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxRepresentativeWork> queryAll(YxRepresentativeWorkQueryCriteria criteria){

        return baseMapper.queryAll(criteria);
        /*return baseMapper.selectList(QueryHelpPlus.getPredicate(YxRepresentativeWork.class, criteria));*/
    }


    @Override
    public void download(List<YxRepresentativeWorkDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxRepresentativeWorkDto yxRepresentativeWork : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("大师id", yxRepresentativeWork.getGrandmasterId());
            map.put("作品", yxRepresentativeWork.getPicture());
            map.put("创建时间", yxRepresentativeWork.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public ApiResult<Page<YxRepresentativeWork>> getRepresentativeWorkPage(Integer grandmasterId, Integer page, Integer limit) {
        Page page1=new Page(page,limit);
        Page<YxRepresentativeWork> paList = this.baseMapper.getRepresentativeWorkPage(page1,grandmasterId);
        return ApiResult.ok(paList);
    }

}
